<?php


namespace app\controller\api;


use app\controller\api\Base;
use app\model\ConfigModel;
use app\model\DeliveryAttachModel;
use app\model\GoodsModel;
use app\model\QuotationModel;
use app\model\QuotationTransportModel;
use app\model\ReleaseScopeModel;
use app\model\SizeModel;
use app\model\VehicleModel;
use app\Request;
use app\validate\QuotationCheck;
use think\App;
use think\facade\Db;
use app\model\DeliveryModel;
use app\model\AddressModel;

class Quotation extends Base
{
    public $token;
    public $version;
    public $lang;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->token = $this->request->header('xzdToken');
        $this->version = $this->request->header('version');
        $this->lang = $this->request->header('lang');
    }


    /**
     * @param Request $request
     * @param QuotationCheck $check
     * @param QuotationModel $quotationModel
     * @notes 报价列表
     * @author: dongdong
     * @date: 2025/1/10 18:39
     */
    public function quotation_list(Request $request, QuotationCheck $check, QuotationModel $quotationModel)
    {
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('quotation')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                $data = $quotationModel->quotation_list($param);
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }

    /**
     * @param Request $request
     * @param QuotationCheck $check
     * @param QuotationTransportModel $quotationTransportModel
     * @notes 车辆报价
     * @author: dongdong
     * @date: 2025/1/14 16:56
     */
    public function transport_quotation(Request $request, QuotationCheck $check, QuotationTransportModel $quotationTransportModel)
    {
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;

            $result = $check->scene('transport_quotation')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                $data = $quotationTransportModel->transport_quotation($param);
                if($data==1) {
                    $this->api_result(200, '请求成功', ['data' => $data]);
                }else{
                    $this->api_result(400, $data, ['data' => $data]);
                }
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }

    /**
     * @param Request $request
     * @param QuotationCheck $check
     * @param QuotationModel $quotationModel
     * @notes 货代报价
     * @author: dongdong
     * @date: 2025/1/14 16:56
     */
    public function freight_quotation(Request $request, QuotationCheck $check, QuotationModel $quotationModel)
    {
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;

            $result = $check->scene('freight_quotation')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                $data = $quotationModel->freight_quotation($param);
                if($data==1){
                    $this->api_result(200, '请求成功', ['data' => $data]);
                }else{
                    $this->api_result(400, $data, ['data' => $data]);
                }
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }


    /**
     * @param Request $request
     * @param QuotationCheck $check
     * @param QuotationModel $quotationModel
     * @notes 车辆选择
     * @author: dongdong
     * @date: 2025/1/14 17:27
     */
    public function car_search(Request $request, QuotationCheck $check, QuotationModel $quotationModel)
    {
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;

            $result = $check->scene('car')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                $data = $quotationModel->car_search($param);
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }


    /**
     * @param Request $request
     * @param QuotationCheck $check
     * @param QuotationModel $quotationModel
     * @notes 货代订单
     * @author: dongdong
     * @date: 2025/1/21 11:44
     */
    public function order(Request $request, QuotationCheck $check, QuotationModel $quotationModel)
    {
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;

            $result = $check->scene('order')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                $data = $quotationModel->quotation_order($param);
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }










    /**
     * @param Request $request
     * @param DeliveryModel $deliveryModel
     * @param QuotationCheck $check
     * @param QuotationModel $quotationModel
     * @param QuotationTransportModel $quotationTransportModel
     * @notes 报价详情
     * @author: dongdong
     * @date: 2025/2/21 17:59
     */
    public function info(Request $request, DeliveryModel $deliveryModel, QuotationCheck $check, QuotationModel $quotationModel, QuotationTransportModel $quotationTransportModel)
    {
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;

            $delivery_id = $param['delivery_id'];
            $user_id = $param['user_id'];

            $demand = $deliveryModel->where(["id" => $delivery_id])->value('demand');


            $result = $check->scene('order')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                if ($demand == '1') {
                    $data = $quotationTransportModel->quotation_info($delivery_id,$user_id);
                } else {
                    $data = $quotationModel->quotation_info($delivery_id,$user_id);
                }
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }


    /**
     * @param Request $request
     * @param DeliveryModel $deliveryModel
     * @param QuotationModel $quotationModel
     * @param QuotationTransportModel $quotationTransportModel
     * @notes 取消报价
     * @author: dongdong
     * @date: 2025/2/21 19:09
     */
    public function cancel_quotation(Request $request, DeliveryModel $deliveryModel, QuotationModel $quotationModel, QuotationTransportModel $quotationTransportModel)
    {
        $param = $request->param();
        $delivery_id = $param['delivery_id']??'';
        $user_id = $param['user_id']??'';

        if($delivery_id){
            $demand = $deliveryModel->where(["id" => $delivery_id])->value('demand');

            if ($demand == '1') {
                $find = $quotationTransportModel->where(['delivery_id' => $delivery_id,'user_id'=>$user_id])->where(['is_deleted' => 0])->find();
                if ($find) {
                    $quotationTransportModel->where(['delivery_id' => $delivery_id,'user_id'=>$user_id])->update(['is_deleted' => 1]);
                    $quotationModel->where(['delivery_id' => $delivery_id,'user_id'=>$user_id])->update(['is_deleted' => 1]);
                    Db::query("update store_delivery_attach set quote_number=quote_number-1 where delivery_id=$delivery_id");
                }
            } else {
                $find = $quotationModel->where(['delivery_id' => $delivery_id,'user_id'=>$user_id])->where(['is_deleted' => 0])->find();
                if ($find) {
                    $quotationModel->where(['delivery_id' => $delivery_id,'user_id'=>$user_id])->update(['is_deleted' => 1]);
                    Db::query("update store_delivery_attach set quote_number=quote_number-1 where delivery_id=$delivery_id");
                }
            }
            $this->api_result(200, '请求成功', ['data' => []]);
        }else{
            $this->api_result(400, '请求失败', ['data' => []]);
        }

    }

}